<%@page language="java" import="com.scalar.chiptrack.finance.PurchaseOrder, com.scalar.chiptrack.finance.PurchaseOrderLineItem, java.util.ArrayList, java.util.Iterator, java.util.HashMap"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>
<META content="MSHTML 5.50.4807.2300" name=GENERATOR>
<SCRIPT language="JavaScript1.2">

    // Added by Basha (07/31/2003)
    function showErrorReportPage()
    {
        newWindow = window.open ('<%=chipTracContext%>/reportProblem.do','reportProblem','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=750,height=400,resizable=no');
        newWindow.focus();
    }


	function PurchaseOrder(){
		this.vendorArray = new Array();
		this.count = 0;
		this.selectedID;
		this.selectedShipObject;
	}

	PurchaseOrder.prototype.addVendor = function(vendor, index){
		this.vendorArray[index] = vendor;
		this.count++;
	}
	PurchaseOrder.prototype.getSelectedID=function(){
		return this.selectedID;
	}

	PurchaseOrder.prototype.getVendor = function(index){
		return this.vendorArray[index];
	}

	function selectVendorID(po){

		var d = document.purchaseOrderForm.vendorID;
		var obj = document.purchaseOrderForm;
		var tempIndex;
		var tempName;
		for(var i=0; i<d.options.length; i++){
        {
			if(d.options[i].selected ){
				tempIndex = d.options[i].value;
				selectedId = tempIndex;
				tempName = d.options[i];
			}
		}
		//for(var x in tempName)
		//alert("Name = "+ tempName.innerText);
		var v = po.getVendor(tempIndex);
		if(v == null){
			obj.vendor.value ="";
			obj.terms.value = "";
			obj.contact.value = "";
			obj.phone.value = "";
			obj.email.value = "";
			obj.fax.value = "";
			var x = obj.shipID;
			x.options[0].text = "Select Shipping Address";
			x.options[0].value = "Select Shipping Address";
			x.options[0].selected = true;
			for(var i=1; i<x.length; i++){
				x.options[i].text = "";
				x.options[i].value = "";
			}
			obj.shipTo.value = "";
		}else{
			//var billAddr = v.billingAddress;
			//alert(billAddr.replace(/"#"/i, "\n"));
			obj.vendor.value = stringUtil(v.billingAddress);
			obj.terms.value = v.terms;
			obj.contact.value = v.contact;
			obj.phone.value = v.phoneNumber;
			obj.email.value = v.email;
			obj.fax.value = v.fax;
			po.selectedShipObjet = v.getShipping();
			setShippingID(v.getShipping(), obj);
			obj.shipTo.value = "";
		}
	}

	function selectShipID(po){
		var d1 = document.purchaseOrderForm.shipID;

		var tempIndex;
		var tempName;
		for(var i=0; i<d1.options.length; i++){
			if(d1.options[i].selected ){
				tempIndex = d1.options[i].value;
			}
		}
		//alert("selectedShipObject = "+po.selectedShipObjet);
		var x = po.selectedShipObjet;
		//alert(x.length);
		for(var j=0; j<x.length; j++){
			//alert("x[j].shiId = " + x[j].shipId+"; tempIndex = "+ tempIndex);
			if(x[j].shipId == tempIndex){
				document.purchaseOrderForm.shipTo.value = stringUtil(x[j].shippingAddress);
			}
		}
		if(tempIndex == "Select Shipping Address"){
			document.purchaseOrderForm.shipTo.value ="";
		}
	}

	function stringUtil(testString){
		var returnString;
		var temp;
		returnString = testString.substring(0, testString.indexOf("#"));
		returnString += "\n";
		temp = testString.substring(returnString.length, testString.length);
		returnString += temp.replace(/#/i, " ");

		return returnString;
	}

	function setShippingID(ship, obj){
		var d = obj.shipID;
		d.length = ship.length+1;
		d.options[0].text = "Select Shipping Address";
		d.options[0].value = "Select Shipping Address";
		d.options[0].selected = true;
		for(var j=1; j<ship.length+1; j++){
			var tempShip = ship[j-1];
			d.options[j].text = tempShip.shipLocationName;
			d.options[j].value = tempShip.shipId;
			//alert("Name = "+j+": ");//d.options[i].name+"; ID = "+d.options[i].value);
		}
	}

	function Vendor(id, name, ba, contact, phone, terms, email, fax){
		this.vendorId = id;
		this.vendorName = name;
		this.billingAddress = ba;
		this.contact = contact;
		this.phoneNumber = phone;
		this.terms = terms;
		this.email = email;
		this.fax = fax;
		this.ship = new Array();
		this.count = 0;
	}

	Vendor.prototype.addShipping = function(shipObject, index){
		this.ship[this.count] = shipObject;
		this.count++;
	}

	Vendor.prototype.getShipping = function(){
		return this.ship;
	}
	function Shipping(id, sln, sa){
		this.shipId = id;
		this.shipLocationName = sln;
		this.shippingAddress = sa;
	}

function cancelAndGoBack() {
    document.location = "jsp/finance/finance.jsp";
 }

</SCRIPT>
</HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<SCRIPT language="JavaScript">

    if (document.layers)
        window.setResizable(false);
	var count = 4;

    function addTextArea() {
     //<table border="0" width="100%"><tr>
        var htmltext = '<tr><table border="0" width="100%"><tr>';
        	htmltext += '<td width="16%" height="19" bgcolor="#DEE1E2"><input type="text" name="item" size=5 value="'+count+ '" class="txfield"></td>';
        	htmltext += '<td width="18%" height="19" bgcolor="#DEE1E2"><input type="text" name="manufacturingPartNumber" size=5 class="txfield"></td>';
        	htmltext += '<td width="18%" height="19" bgcolor="#DEE1E2"><input type="text" name="vendorPartNumber" size=5 class="txfield"></td>';
        	htmltext += '<td width="16%" height="19" bgcolor="#DEE1E2"><input type="text" name="requestDate" size=5 class="txfield"></td>';
        	htmltext += '<td width="17%" height="19" bgcolor="#DEE1E2"><input type="text" name="quantity" size=5 value="0" class="txfield"></td>';
        	htmltext += '<td width="33%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font><input type="text" name="rate" size=5 value="0.0" class="txfield"></td>';
        	htmltext += '<td width="20%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font><input type="text" name="amount" size=5 value="0.0" class="txfield"></td>';
        	htmltext += '</tr></table>';

        var pa=document.getElementById("font1");
        pa.insertAdjacentHTML('BeforeEnd',htmltext);

		count++;
    }

    function submitPage(action)
    {
        if(action == 'UPDATE')
            document.purchaseOrderForm.action.value = "Update";
        else if(action == 'SAVE')
            document.purchaseOrderForm.action.value = "SAVE";

        document.purchaseOrderForm.submit();
    }
    function setRowID(count){
    	alert("Count = " + count);
    }
</SCRIPT>

<SCRIPT language="JavaScript1.2">
	var po = new PurchaseOrder();
<%
		ArrayList list = (ArrayList)request.getAttribute("shippingList");
		Iterator it = list.iterator();
		while(it.hasNext()){
			PurchaseOrder p = (PurchaseOrder)it.next();
			HashMap hm = p.getShipping();
			ArrayList shipList = (ArrayList)hm.get(p.getVendorID());
			Iterator tempIt = shipList.iterator();
%>
		temp = new Vendor(<%= p.getVendorID() %>,
		                        '<%= p.getVendorName()%>',
		                        '<%= p.getBillingAddress()%>',
								'<%= p.getContact() %>', 
								'<%= p.getPhone() %>', 
								'<%= p.getTerms()%>', 
								'<%= p.getEmail()%>',
								'<%= p.getFax()%>');
<%
			while(tempIt.hasNext()){
				PurchaseOrder.POShipping pos = (PurchaseOrder.POShipping)tempIt.next();
%>
				
				temp.addShipping(new Shipping(<%= pos.getShipID()%>,'<%= pos.getShipLocationName()%>','<%= pos.getShipTo()%>'), <%= p.getVendorID() %>);
<%
			}
%>
			po.addVendor(temp, <%= p.getVendorID() %>);			
			temp = null;
			
<%
		}
%>
	
</SCRIPT>
<!-- Top Header Start-->

<%@include file="/jsp/includes/header.jsp"  %> 

<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
	<TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->

<%@include file="/jsp/finance/purchase_order_menu.jsp" %>

</TD>
<!-- end of the side navigations -->

<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>
<html:html>
<!-- from here your body starts -->

<center><span class="NArialW">Purchase Order Entry</span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20>
<table border="0" width="90%" align=right>

<tr><td colspan=6><chiptrack:info styleClass="NArialL" /></td></tr>
<tr><td colspan=6><chiptrack:errors styleClass="NArialL" /></td></tr>

<tr><td>
  <table border="0" width="98%">
  <tr><td colspan=5><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
  
  <html:form name="purchaseOrderForm" action="/purchaseOrder.do" method="post" type="com.scalar.chiptrack.finance.forms.PurchaseOrderForm">
  <tr>
    <td width="32%" align="left"><font class="NArialL">Purchase Order No</font></td>
    <td width="30%" align="left"><html:text property="purchaseOrderNumber" size="20" styleClass="txfield" />
      </td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">Change No</font></td>
    <td width="36%" align="left"><html:text property="changeOrderNumber" size="20" styleClass="txfield" />
      </td>
  </tr>
  <tr>
    <td width="32%" align="left"><font class="NArialL">Vendor ID</font></td>
    <td width="30%" align="left">
    	<html:select name="purchaseOrderForm" size="1" property="vendorID" styleClass="txfield" onchange="javascript:selectVendorID(po)">
    		<html:option value="Select Vendor" />
        	<html:options collection="vendorList" property="value" labelProperty= "label"  />
      	</html:select>
    </td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">Ship ID</font></td>
    <td width="36%" align="left">
    	<!-- <select size="1" name="shipID" id="shipHighLight" class="txfield" >
    		<option >Select Shipping Address</option>
    		</select> -->
     	<html:select name="purchaseOrderForm" size="1" property="shipID" styleClass="txfield" onchange="javascript:selectShipID(po)">
    		<html:option value="Select Ship Location" />
        	<html:options collection="shipIDList" property="value" labelProperty= "label"  />
      	</html:select>
      	
     </td>
  </tr>
  <tr>
    <td width="32%" align="left"><font class="NArialL">Vendor</font></td>
    <td width="30%" align="left"><textarea rows="3" name="vendor" cols="30" class="txfield"></textarea></td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">Ship To</font></td>
    <td width="36%" align="left"><textarea rows="3" name="shipTo" cols="30" class="txfield"></textarea></td>
  </tr>

  <tr>
    <td width="32%" align="left"><font class="NArialL">Contact</font></td>
    <td width="30%" align="left"><input type="text" name="contact" size="20" class="txfield"></td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">Email</font></td>
    <td width="36%" align="left"><input type="text" name="email" size="20" class="txfield"></td>
  </tr>

  <tr>
    <td width="32%" align="left"><font class="NArialL">Phone</font></td>
    <td width="30%" align="left"><input type="text" name="phone" size="20" class="txfield"></td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">Fax</font></td>
    <td width="36%" align="left"><input type="text" name="fax" size="20" class="txfield"></td>
  </tr>

  <tr>
    <td width="32%" align="left"><font class="NArialL">Terms</font></td>
    <td width="30%" align="left"><input type="text" name="terms" size="20" class="txfield"></td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"><font class="NArialL">FOB</font></td>
    <td width="36%" align="left"><input type="text" name="fob" size="20" class="txfield"></td>
  </tr>

  <tr>
    <td width="32%" align="left"><font class="NArialL">Ship Via</font></td>
    <td width="30%" align="left"><select size="1" name="shipVia" class="txfield">
        <option>Select Any Option</option>
      </select></td>
    <td width="11%" align="left"></td>
    <td width="22%" align="left"></td>
    <td width="36%" align="left"></td>
  </tr>
  </table>
  </td></tr>
  <tr><td>
  <table border="0" width="98%" bordercolor="#FFFFFF">
    <tr>
      <td width="17%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Item</font></td>
      <td width="18%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Manufacturing Part
        Number&nbsp;</font></td>
      <td width="18%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Vendor Part
        No.</font></td>
      <td width="17%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Request
        Date</font></td>
      <td width="17%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Quantity</font></td>
      <td width="33%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Cost/Unit</font></td>
      <td width="20%" height="13" bgcolor="#993333" align="center"><font class="NArialL"><font color="#FFFFFF">Amount</font></td>
    </tr>

 <%
 		ArrayList lineItem = (ArrayList)request.getAttribute("purchaseOrderLineItem");
 		Iterator iterator = lineItem.iterator();
 		int i=0;
 		while(iterator.hasNext()){
 		PurchaseOrderLineItem poli = (PurchaseOrderLineItem)iterator.next();
 %>
	<tr>
      <td width="17%" height="19" bgcolor="#DEE1E2">

      	<input type=text name="item" size='5' class="txfield" value ="<%= poli.getItem()%>" ></td>
      <td width="18%" height="19" bgcolor="#DEE1E2">
 		<input type=text name="manufacturingPartNumber" size='5' class="txfield" value="<% if(poli.getManufacturingPartNumber() != null)poli.getManufacturingPartNumber();%>"></td>
      <td width="18%" height="19" bgcolor="#DEE1E2">
      	<input type=text name="vendorPartNumber" size='5' class="txfield" value="<% if(poli.getVendorPartNumber()!= null)poli.getVendorPartNumber();%>"></td>
      <td width="17%" height="19" bgcolor="#DEE1E2">
      	<input type=text name="requestDate" size='5' class="txfield" value="<% if(poli.getRequestDate() != null)poli.getRequestDate();%>"></td>
      <td width="17%" height="19" bgcolor="#DEE1E2">
      	<input type=text name="quantity" size='5' class="txfield" value="<% if(poli.getQuantity() != 0)poli.getQuantity();%>" onchange="javascript:setRowID(<%= i++%>)" ></td>
      <td width="33%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font>
      	<input type=text name="rate" size='5' class="txfield" value="<% if(poli.getRate() != 0)poli.getRate();%>"></td>
      <td width="20%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font>
      	<input type=text name="amount" size='5' class="txfield" value="<% if(poli.getAmount() != 0)poli.getAmount();%>"></td>
    </tr>
<%
 		}
 %>
 
 
    <!--<logic:iterate id="purchaseOrderLineItem" name="purchaseOrderLineItem">
    <tr>
      <td width="17%" height="19" bgcolor="#DEE1E2">

      	<html:text name="purchaseOrderLineItem" property="item" size='5' styleClass="txfield" disabled="true" /></td>
      <td width="18%" height="19" bgcolor="#DEE1E2">
      	<html:text name="purchaseOrderLineItem" property="manufacturingPartNumber" size='5' styleClass="txfield" /></td>
      <td width="18%" height="19" bgcolor="#DEE1E2">
      	<html:text name="purchaseOrderLineItem" property="vendorPartNumber" size='5' styleClass="txfield" /></td>
      <td width="17%" height="19" bgcolor="#DEE1E2">
      	<html:text name="purchaseOrderLineItem" property="requestDate" size='5' styleClass="txfield" /></td>
      <td width="17%" height="19" bgcolor="#DEE1E2">
      	<html:text name="purchaseOrderLineItem" property="quantity" size='5' styleClass="txfield" onchange="javascript:setRowID()" /></td>
      <td width="33%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font>
      	<html:text name="purchaseOrderLineItem" property="rate" size='5' styleClass="txfield" /></td>
      <td width="20%" height="19" bgcolor="#DEE1E2"><font class="NArialL">$</font>
      	<html:text name="purchaseOrderLineItem" property="amount" size='5' styleClass="txfield" /></td>
    </tr>
    </logic:iterate>
    -->
    <tr><td colspan='7' align="left"><div id="font1"></div></TD></TR>
    <tr>
    <td width="17%" height="18"></td>
    <td width="18%" height="18"></td>
    <td width="18%" height="18"></td>
    <td width="17%" height="18"><font class="NArialL">Total</font></td>
    <td width="17%" height="18"><input type="text" name="totalQuantity" size="10" class="txfield"></td>
    <td width="33%" height="18"></td>
    <td width="20%" height="18" align=right><input type="text" name="totalAmount" size="10" class="txfield"></td>
  </tr>

     <tr>
           <td colspan='6' align='right'>
                <a href="javascript:addTextArea();">
                    <img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border=0></a>
               <!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <a href="javascript:submitPage('UPDATE');">
                <img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border='0'></a> -->
            </td>
     </tr>
   
   <tr><td colspan=7><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
	   <tr><td colspan=7 align=center>
	   <a href="javascript:submitPage('SAVE')">
	   <img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border=0></a>
	   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	  <html:link styleClass="NArialL" href="javascript:cancelAndGoBack();" >
	   <img src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" border=0></html:link></td></tr>
    </table>
  </td></tr>

</table>

<input type="hidden" name="action" value="">
</html:form>
</html:html>
<!-- till here -->
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->

<%@include file="/jsp/includes/footer.jsp" %>

<!-- footer end here -->
</BODY>
</HTML>
